<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/12
 * Time: 10:35
 */

namespace zhtfrontend\controllers;

use common\models\datas\AccountRecord;
use common\models\datas\Admin;
use common\models\datas\CustomerBranchaccount;
use doumiaoduo\buildform\BuildForm;
use yii\web\NotFoundHttpException;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use yii\web\Response;
use common\models\datas\Customer;
use yii\helpers\Json;
use yii\data\Pagination;

class AccountrecordController extends BaseController
{

    /**
     * 会员管理-会员金额变动
     */
    public function actionIndex()
    {
        $param = \Yii::$app->request->get();
        $site_id = Fun::getSiteId();
        $accountrecord = AccountRecord::find()->where(['site_id' => $site_id])->orderBy('id DESC');
        if (!empty($param)) {
            $search_data = array_filter($param);

            if (!empty($search_data['customer_name'])) {
                $customer = Customer::find()->select('id')->Where(['like', 'name', $search_data['customer_name']])->column();
                $accountrecord->andWhere(['in', 'customer_id', $customer]);
            }
            if (!empty($search_data['mobile'])) {
                $customer = Customer::find()->select('id')->Where(['mobile' => $search_data['mobile']])->column();
                $accountrecord->andWhere(['customer_id' => $customer]);
            }
            if (!empty($search_data['admin_name'])) {
                $user = Admin::find()->select('id')->Where(['like', 'username', $search_data['admin_name']])->column();
                $accountrecord->andWhere(['in', 'user_id', $user]);
            }
            if (!empty($search_data['state'])) {
                $accountrecord->andWhere(['state' => $search_data['state']]);
            }
            if (!empty($search_data['created_at_begin']))
                $accountrecord->andWhere(['>=', 'created_at', strtotime($search_data['created_at_begin'])]);

            if (!empty($search_data['created_at_end']))
                $accountrecord->andWhere(['<=', 'created_at', strtotime($search_data['created_at_end'])]);
        }
        $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
        $pages = new Pagination(['totalCount' => $accountrecord->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $accountrecord->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['customer_name', 'admin_name', 'full_activities_name', 'operate']);
        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }

    /**
     * 修改
     */
    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $param = \Yii::$app->request->get();
        $model = AccountRecord::findOne($param['id']);
        $form = new BuildForm('@app/components/forms/accountrecordchargeForm.php');
        $form->loadModel(['accountrecord' => $model]);
        $form->loadDefaultValues();
        $site_id = Fun::getSiteId();
        $customer = Customer::findOne($param['id']);
        if ($customer == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '用户不存在'));
        }
        $BAccout = CustomerBranchaccount::find()->where(['customer_id' => $param['id'], 'site_id' => $site_id])->one();
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['accountrecord/index']));
            } else {
                return $this->redirectMessage('修改失败', Fun::url(['accountrecord/index']));
            }
        }
        return $this->render('update', ['form' => $form->render(), 'model' => $model, 'customer' => $customer, 'BAccout' => $BAccout]);
    }

    /**
     * 充值统计
     */
    public function actionRecharge()
    {
        $param = \Yii::$app->request->get();
        $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
        $accountrecord = AccountRecord::find()->where(['state' => 1, 'site_id' => Fun::getSiteId()])->orderBy('id DESC');
        if (!empty($param)) {
            $search_data = array_filter($param);
            if (!empty($search_data['name'])) {
                $customer = Customer::find()->select('id')->Where(['like', 'name', $search_data['name']])->column();
                if ($customer != null)
                    $accountrecord->andWhere(['in', 'customer_id', $customer]);
            }
            if (!empty($search_data['tel'])) {
                $customer_id = Customer::find()->select('id')->Where(['like', 'mobile', $search_data['tel']])->column();
                if ($customer_id != null)
                    $accountrecord->andWhere(['in', 'customer_id', $customer_id]);
            }
            if (isset($search_data['searchWay']) && $search_data['searchWay'] != null) {
                if ($search_data['searchWay'] == 2) { //按日搜索
                    $accountrecord->andWhere(['between', 'created_at', strtotime(date('Y-m-d', strtotime($search_data['searchDate']))), strtotime(date('Y-m-d', strtotime("+1 day", strtotime($search_data['searchDate']))))]);
                } else { //按月搜索
                    $accountrecord->andWhere(['between', 'created_at', strtotime(date('Y-m', strtotime($search_data['searchDate']))), strtotime(date('Y-m', strtotime("next month", strtotime($search_data['searchDate']))))]);
                }
            }

        }
        $totals = $accountrecord->sum('cash');
        $pages = new Pagination(['totalCount' => $accountrecord->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $accountrecord->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['customer_name', 'admin_name']);
        $content = "<div class='yx-total-box'><h4 class='yx-total1 yx-floatl '>总计：<span class='yx-color2cb4f3 yx-font16 yx-mr5'>{$accountrecord->count()}</span>条</h4><h4 class='yx-total1 yx-floatl'>金额总计：<span class='yx-color2cb4f3 yx-font16 yx-mr5'>{$totals}</span>元</h4><div class='yx-clear'></div></div>";
        return $this->render('recharge', ['data' => $array, 'pages' => $pages, 'content' => $content]);
    }
}